package com.iyunwen.datatool.dataexport;

import com.iyunwen.datatool.common.annotations.EventTypeAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 数据加载策略控制器
 * @author: Zhu.Guodong
 * @create: 2020-03-19 09:36
 * @version:
 */
@Component
public class DataExportContext {

    private final Map<Integer, DataExport> dataExportMap = new ConcurrentHashMap<>();

    @Autowired
    public void strategyInterface(Map<String,DataExport> strategyMap){
        for (Map.Entry<String, DataExport> entry : strategyMap.entrySet()) {
            DataExport value = entry.getValue();
            EventTypeAnnotation declaredAnnotation = value.getClass().getDeclaredAnnotation(EventTypeAnnotation.class);
            dataExportMap.put(declaredAnnotation.eventType().getIndex(), value);
        }
    }

    /**
     * 策略选择
     * @param eventType
     * @return
     */
    public DataExport getDataExport(int eventType) {
        if (dataExportMap.containsKey(eventType)) {
            return dataExportMap.get(eventType);
        }
        return null;
    }
}
